import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', '$state', 'toDoService', 'ngDialogService')
class ToDoController {
  constructor() {
    this.selectedIndex = 0;
    this.mapParams = {
      orgId: $rootScope.orgId || storageApi.get('orgId'),
    };

    this.$table = {
      fetchTimes: 0,
      isTableLoading: false,
      pagination: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };

    this.init();

    this.$paginationQuery();
  }

  $paginationQuery(type, index) {
    this.selectedIndex = index === undefined ? 0 : index;
    this.mapParams.todoType = type === undefined ? '0' : type;
    this.$query();
  }

  $query(param) {
    this.toDoService
      .query({
        mapParams: this.mapParams,
        page: this.$table.pagination.currentPage,
        size: this.$table.pagination.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.listData = res.rows;
        this.$table.pagination.totalItems = res.total;
        this.$table.isTableLoading = false;
        this.$table.fetchTimes = 0;
        if (res.total < 1) {
          this.$table.fetchTimes += 1;
        }
      });
  }

  /*eslint-disable */
  getType(type) {
    switch (type) {
      case '1':
        return '人员';
        break;
      case '2':
        return '设备';
        break;
      case '3':
        return '流程';
        break;
      case '4':
        return '巡查';
        break;
      default:
        break;
    }
  }

  // ui-sref="processTableTodo({piId: item.piId, todoId: item.id, tableId: item.businessId, name: '方案审查'})"
  $goPageDetail(param) {
    if (param.todoType === '1') {
      this.$state.go('workerInfoDetail', { id: param.serviceId });
    }
    if (param.todoType === '2') {
      this.$state.go('deviceInfoDetail', { id: param.serviceId });
    }
    if (param.todoType === '3') {
      this.$state.go('processTableTodo', { piId: param.piId, todoId: param.id,
        tableId: param.serviceId, name: param.documentTypeName });
    }
    if (param.todoType === '4') {
      this.$state.go('patrolDetail', { id: param.serviceId });
    }
  }

  init() {
    this.tabData = [
      {
        type: '0',
        name: '全部',
      },
      {
        type: '1',
        name: '人员',
      },
      {
        type: '2',
        name: '设备',
      },
      {
        type: '3',
        name: '流程',
      },
      {
        type: '4',
        name: '巡查',
      },
    ];
  }

}

export default ToDoController;
